<!-- 发布nav-->
<template>
  <div class="fire" slot-scope="">
    <!-- <van-nav-bar :title="fnTitle" left-text="返回" right-text="发送" left-arrow fixed placeholder @click-left="onClickLeft" @click-right="onClickRight(info)">
    </van-nav-bar> -->
    <van-notify v-model="show" type="success">
      <van-icon name="bell" style="margin-right: 4px" />
      <span>发送成功!</span>
    </van-notify>
    <van-notify v-model="show2" type="warning">
      <van-icon name="bell" style="margin-right: 4px" />
      <span>请完整填写信息!</span>
    </van-notify>
    <van-nav-bar :title="fnTitle" left-text="返回" left-arrow @click-left="onClickLeft" @click-right="onClickRight">
      <template #right>
        <button id="send_btn">发送</button>
      </template>
    </van-nav-bar>
  </div>
</template>

<script>
import Vue from 'vue'
import axios from 'axios'
Vue.use(axios)
export default {
  name: 'NavBar2',
  props: {
    fnTitle: {
      type: String,
      required: true
    }
  },
  created() {
    this.confirm_login()
  },
  data() {
    return {
      show: false,
      show2: false
    }
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1)
    },
    showNotify() {
      this.show = true
      setTimeout(() => {
        this.show = false
        this.$router.replace('/home')
      }, 2000)
    },
    confirm_login() {
      const token = sessionStorage.getItem('token')
      if (token == null) {
        this.$router.replace('/login')
      }
    },
    showNotify2() {
      this.show2 = true
      setTimeout(() => {
        this.show2 = false
      }, 2000)
    },
    onClickRight() {
      var tar = this.fnTitle
      if (tar === '发布吐槽') {
        console.log('发布吐槽')
        this.firefun()
      } else if (tar === '发布表白') {
        console.log('发布表白')
        this.likefun()
      } else if (tar === '发布闲置') {
        console.log('发布闲置')
        this.coinfun()
      } else if (tar === '发布盲盒') {
        this.UpGift()
      } else if (tar === '发布拼伞') {
        this.UpUmbrella()
      } else if (tar === '乐跑信息') {
        this.UpRun()
      }
    },
    likefun() {
      const that = this
      var token = sessionStorage.getItem('token')
      var p1 = sessionStorage.getItem('Likepicture1')
      var p2 = sessionStorage.getItem('Likepicture2')
      var text = sessionStorage.getItem('LikeText')
      axios
        .post('http://39.96.36.251/api/love/add', {
          token: token,
          picture1: p1,
          picture2: p2,
          text: text,
          statue: 1
        })
        .then(function (res) {
          console.log(res)
          if (res.data.code == 200) {
            that.showNotify()
            sessionStorage.removeItem('Likepicture1')
            sessionStorage.removeItem('Likepicture2')
            sessionStorage.removeItem('LikeText')
          } else if (res.data.code == 422) {
            that.showNotify2()
          }
        })
        .catch(function (err) {
          console.log(err)
        })
    },
    firefun() {
      //点击发送
      const that = this
      var p1 = sessionStorage.getItem('Firepicture1')
      var p2 = sessionStorage.getItem('Firepicture2')
      var text = sessionStorage.getItem('FireText')
      var token = sessionStorage.getItem('token')
      axios
        .post('http://39.96.36.251/api/tucao/add', {
          token: token,
          picture1: p1,
          picture2: p2,
          text: text,
          statue: 1
        })
        .then(function (res) {
          console.log(res)
          if (res.data.code == 200) {
            that.showNotify()
            sessionStorage.removeItem('Firepicture1')
            sessionStorage.removeItem('Firepicture2')
            sessionStorage.removeItem('FireText')
            sessionStorage.removeItem('FireMeme')
            sessionStorage.removeItem('FireTopic')
          } else if (res.data.code == 422) {
            that.showNotify2()
          }
        })
        .catch(function (err) {
          console.log(err)
        })
    },
    coinfun() {
      const that = this
      var token = sessionStorage.getItem('token')
      var p1 = sessionStorage.getItem('Coinpicture1')
      var p2 = sessionStorage.getItem('Coinpicture2')
      var text = sessionStorage.getItem('CoinText')
      var price = sessionStorage.getItem('CoinPrice')
      axios
        .post('http://39.96.36.251/api/idle/add', {
          price: price,
          token: token,
          picture1: p1,
          picture2: p2,
          text: text,
          statue: 1
        })
        .then(function (res) {
          console.log(res)
          if (res.data.code == 200) {
            that.showNotify()
            sessionStorage.removeItem('Coinpicture1')
            sessionStorage.removeItem('Coinpicture2')
            sessionStorage.removeItem('CoinText')
            sessionStorage.removeItem('CoinPrice')
          } else if (res.data.code == 422) {
            that.showNotify2()
          }
        })
        .catch(function (err) {
          console.log(err)
        })
    },
    // 乐跑发布接口调用
    async UpRun() {
      var text = sessionStorage.getItem('RunText')
      const { data: res } = await this.$http({
        method: 'post',
        url: '/api/run/happyruncreate',
        data: this.$qs.stringify({
          // 这里是发送给后台的数据
          id: 20,
          text: text
        })
      })
      if (text === '') {
        alert('内容未填')
        return
      }
      if (res.code !== 200) {
        alert('请先点击保存')
        return
      }
      if (text === '') {
        alert('内容未填')
        return
      }
      sessionStorage.removeItem('RunText')
      alert('发布成功！')
      this.$router.push('/run')
    },
    // 拼伞发布接口调用
    async UpUmbrella() {
      var text = sessionStorage.getItem('UmberText')
      var location = sessionStorage.getItem('Location')
      const { data: res } = await this.$http({
        method: 'post',
        url: '/api/umbrella/umbrellacreate',
        data: this.$qs.stringify({
          // 这里是发送给后台的数据
          // token-----id
          id: 20,
          text: text,
          location: location
        })
      })
      if (text === '' || location === '') {
        alert('内容或者位置未填')
        return
      }
      if (res.code !== 200) {
        // console.log(res)
        alert('请先点击保存')
        return
      }
      if (text === '' || location === '') {
        alert('内容或者位置未填')
        return
      }
      sessionStorage.removeItem('UmberText')
      sessionStorage.removeItem('Location')
      alert('发布成功！')
      // console.log(res)
      this.$router.push('/umbrella')
    },
    // 盲盒上传接口调用
    async UpGift() {
      var picture1 = sessionStorage.getItem('picture1')
      var picture2 = sessionStorage.getItem('picture2')
      var text = sessionStorage.getItem('GiftText')
      const { data: res } = await this.$http({
        method: 'post',
        url: '/api/box/add',
        data: this.$qs.stringify({
          // 这里是发送给后台的数据
          picture1: picture1,
          picture2: picture2,
          text: text
        })
      })
      if (text === '' || picture1 === '' || picture2 === '') {
        alert('内容或照片未填完善')
        return
      }
      if (res.code !== 200) {
        alert('请先点击保存')
        return
      }
      if (text === '' || picture1 === '' || picture2 === '') {
        alert('内容或照片未填完善')
        return
      }
      sessionStorage.removeItem('picture1')
      sessionStorage.removeItem('picture2')
      sessionStorage.removeItem('GiftText')
      alert('分享成功')
      this.$router.push('/gift')
    }
  }
}
</script>

<style scoped>
#send_btn {
  background-color: red;
  color: white;
  border: none;
  outline: none;
  padding: 3px 15px;
  border-radius: 30px;
}
</style>
